/*{
  let ajax=function(callback){
    console.log("start");
    setTimeout(function(){
      callback&&callback.call()
    },1000);
  }
  ajax(function(){
    console.log('timeout1');
  })
}
{
  let ajax=function(){
    console.log('zhiixng2');
    return new Promise(function(resolve,reject){
      setTimeout(function(){
        resolve()
      },1000)
    })
  };
  ajax().then(function(){
    console.log('promise','timeout2');
  })
}*/
/*{
  let ajax=function(){
    console.log('zhiixng3');
    return new Promise(function(resolve,reject){
      setTimeout(function(){
        resolve()
      },1000)
    })
  };

  ajax().then(function(){
    return new Promise(function(resolve,reject){
      setTimeout(function(){
        resolve()
      },2000)
    })
  }).then(function(){
    console.log('timeout3');
  })
}*/
/*{
  let ajax=function(num){
    console.log('zhiixng4');
    return  new Promise(function(resolve,reject){
      if (num>5) {
        resolve();
      } else {
        throw new Error('error');
      }
    })
  }

  ajax(3).then(function(){
    console.log("log",3);
  }).catch(function(err){
    console.log('catch',err)
  })
}*/

{
  function loadImg(src){
    return new Promise((resolve,reject)=>{
      let img=document.createElement('img');
      img.src=src;
      img.onload=function(){
        resolve(img);
      }
      img.onerror=function(err){
        reject(err);
      }
    })
  }

  function showImgs(imgs){
    imgs.forEach(function(img){
      document.body.appendChild(img);
    })
  }

  Promise.all([
    loadImg('https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/logo_white_fe6da1ec.png'),
    loadImg('http://i4.buimg.com/56751/df1ef0720bea6832.png'),
    loadImg('http://i4.buimg.com/56751/df1ef0720bea6833.png')
    ]).then(showImgs)

}

{
  function loadImg(src){
    return new Promise((resolve,reject)=>{
      let img=document.createElement('img');
      img.src=src;
      img.onload=function(){
        resolve(img);
      }
      img.onerror=function(err){
        reject(err);
      }
    })
  }

  function showImgs(img){
    let p=document.createElement('p');
    p.appendChild(img)
    document.body.appendChild(p)
  }

  Promise.race([
    loadImg('https://ss2.baidu.com/6ONYsjip0QIZ8tyhnq/it/u=1404110873,1552586879&fm=80&w=179&h=119&img.JPEG'),
    loadImg('https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/logo_white_fe6da1ec.png'),
    loadImg('http://i4.buimg.com/56751/df1ef0720bea6833.png')
    ]).then(showImgs)

}